<section class="book_form">
    <div class="container">
        <h4>BOOK NOW</h4>
        <?php
        $form = $this->beginWidget('GxActiveForm', array(
            'id' => 'shoppingCart-form',
            //'action' => Yii::app()->createUrl('shoppingCart/create'),
            'enableClientValidation' => true,
            'clientOptions' => array('validateOnSubmit' => true, 'validateOnChange' => true, 'enableAjaxValidation' => true,
            ),
        ));
        ?>
        <div class="form-group">
            <!--                               classcourse type means class or course -->
            <input name="class_course_type" id="class_course_type" value="<?php echo $classcoursetype; ?>"  type="hidden" />
            <?php /* show  all vanue name */ ?>
            <?php
            echo CHtml::dropDownList('ShoppingCart[venue_id]', '', $venueDetails, array('class' => 'venueDetail form-control book-control'));
            ?>           
            <span class="locate-arrow2"></span>
        </div>
        <div class="form-group">           
            <div class="my-area" id="showVenueDataResult"></div>
        </div>
        <div class="form-group">
            <?php echo CHtml::dropDownList('batch_start_date', '', $batchStartDateList, array('class' => 'batchDetail span2 form-control book-control')); ?>
            <span class="locate-arrow2"></span>
            <div id="batchDateError"></div>
        </div>
        <?php if ($classcoursetype == "class") { ?>
            <div class="form-group" id="batchtimeval">
                <?php echo CHtml::dropDownList('batch_starttime', '', $batchStartTimeList, array('class' => 'batchTime form-control book-control')); ?>
                <span class="locate-arrow2"></span>
                <div id="batchTimeError"></div></div>
        <?php } ?>
        <div class="form-group" id="batchticketval">
            <?php echo CHtml::dropDownList('batch_batchseats', '', $batchseatList, array('class' => 'batchSeat form-control book-control')); ?>
            <span class="locate-arrow2"></span>
            <div id="batchSeatError"></div>
        </div>
        <div class="form-group totalBatchseatsPrice">
            <?php echo ClassBatch::getBatchPriceBySeats($batchId, 1); ?>                 
        </div>
        <div class="form-group" id="gifClassTextBoxdiv" style="display:none">
            <input name="GifttofriendWorkshop[gift_to_user_email]" id="gift_to_user_email" class="book-control form-control" value=""  type="text" placeholder="Please Enter friend email address"/>
            <div class="errorMessage" id="error_gift_to_user_email"></div>
            <input name="gifClassTextBox" id="gifClassTextBox" value=""  type="hidden" />
        </div>
        <div class="booking-form1">
            <div id="selected_venue_date_error" class="errorMessage"></div>
            <span id="shoppingcartloader" class="booking-loader" style="display: none"><img src="<?php echo ThemePath(); ?>/images/myskill.gif">"></span>
            <?php 
            $closeClass=  ClassBatch::getClassClose($classid);
            if ($seats > 0 && $closeClass!='close') { ?> <input type="button" value="book now" id="shopping-cartbook"onclick="return validationBookNow();"> <?php } else { ?>
                <input type="button" name="close" value="session close" class="closeclass">
            <?php } ?>
            <?php $this->endWidget(); ?>
        </div>


    </div><!--form container-->
</section>

<script language="javascript">
            function validationBookNow() {
                var d = new Date();
                var month = d.getMonth() + 1;
                var day = d.getDate();
                var currentdate = d.getFullYear() + '-' +
                        (('' + month).length < 2 ? '0' : '') + month + '-' +
                        (('' + day).length < 2 ? '0' : '') + day;
                var checkbatchTime = $("#batch_starttime option:selected").text();
                var checkbookSeat = $("#batch_batchseats").val();
                var batchId = $("#batch_starttime").val();
                var class_course_type = $("#class_course_type").val();
                var bookDateId = $("#batch_start_date option:selected").text()
                var batchprice = $("#totalPriceId").val();
                var giftemailText = $("#gift_to_user_email").val();
                if ($("#gifClassTextBox").val( ) == 1) {
                    if (giftemailText != '') {
                        var pattern = /^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*@[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/;
                        if (!pattern.test(giftemailText)) {
                            $("#error_gift_to_user_email").text('Please Enter valid email address.');
                            return false;
                        }
                    } else if (giftemailText == '') {
                        $("#error_gift_to_user_email").text('Please Enter gift user email address.');
                        return false;
                    }
                }

                if (currentdate > bookDateId) {
                    $("#selected_venue_date_error").text("Please select another or another batch.because this batch has closed. ")
                    return false;
                }
                if (bookDateId)
                    if (batchId != '' && class_course_type != '' && checkbookSeat != '') {
                        jQuery.ajax({
                            'type': 'POST',
                            'url': "<?php echo Yii::app()->createAbsoluteUrl("shoppingCart/create"); ?>",
                            'data': 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + "&class_course_type=" + class_course_type + "&ShoppingCart[batch_id]=" + batchId + "&ShoppingCart[booking_seats]=" + checkbookSeat + "&ShoppingCart[booking_date]=" + bookDateId + "&ShoppingCart[booking_price]=" + batchprice + "&ShoppingCart[batchTime]=" + checkbatchTime + "&GifttofriendWorkshop[gift_to_user_email]=" + giftemailText,
                            'beforeSend': function(jqXHR, settings) {
                                $("#shoppingcartloader").show();
                            },
                            'success': function(data) {
                                $("#shoppingcartloader").hide();
                                var obj = jQuery.parseJSON(data);
                                if (obj.status == 1) {
                                    $('.shopingaddvaluecartvalue').html(obj.shopngcartitem);
                                    viewshopingcart(1);
                                }
                            },
                            'cache': false
                        });
                    }
            }

            $(document).ready(function(e) {
                getVenueDetails($("#ShoppingCart_venue_id").val());
            })
            $('.venueDetail').change(function() {
                var venueId = $("#ShoppingCart_venue_id").val();
                getVenueDetails(venueId);
                venueBatchChange(venueId);
            })
            function venueBatchChange(venueId) {
                $.ajax({
                    type: 'POST',
                    url: '<?php echo Yii::app()->createUrl("/ClassBatch/BatchDeatils"); ?>',
                    dataType: 'JSON',
                    data: 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + '&venueId=' + venueId + '&classId=<?php echo $classid; ?>',
                    success: function(data) {
                        var batchstart = '';
                        $("#batch_start_date").html('');
                        batchTimeinselectBox(data);
                        $.each(data, function(i, obj) {
                            batchstart += "<option value='" + obj.id + "'>" + obj.start_date + "</option>";
                        });
                        $("#batch_start_date").html(batchstart);
                        onstartdateChange($('.batchDetail').val());

                    }
                });
            }
            $('.batchDetail').change(function() {
                onstartdateChange($('.batchDetail').val());
            })
            $('#batch_batchseats').change(function() {
                var batchId = $("#batch_start_date").val();
                var seats = $('#batch_batchseats').val();
                getpricewithDiscount(batchId, seats);
            })

            function batchTimeinselectBox(data) {
                var batchstarttime = '';
                $.each(data, function(i, obj) {
                    if (i == 0) {
                        batchSeatinselectBox(obj.id, obj.seats);
                    }
                    batchstarttime += "<option value='" + obj.id + "'>" + obj.start_time + "</option>";
                });
                $("#batch_starttime").html('');
                $("#batch_starttime").html(batchstarttime);

            }


            function getVenueDetails(venueIndex) {
                $.post("<?php echo Yii::app()->createUrl("/classVenue/SelectedVenue"); ?>", {'YII_CSRF_TOKEN': $('input[name="YII_CSRF_TOKEN"]').val(), venueId: venueIndex, user_id: 2},
                function(result) {
                    $("#showVenueDataResult").html(result);
                    $("#selected_venue_date_error").text("");
                });
            }

            //get selected batch time //
            function batchSeatinselectBox(batchId, seats) {
                var batchseats = '';
                for (i = 1; i <= seats; i++) {
                    batchseats += "<option value='" + i + "'>" + i + "</option>";
                }
                $("#batch_batchseats").html(batchseats);
                getpricewithDiscount(batchId, 1);
                getpricewithDiscount(batchId, $('.batchSeat').val())
            }


            //get get price with discount and early discount//
            function getpricewithDiscount(batchId, seats) {
                $.ajax({
                    type: 'POST',
                    url: '<?php echo Yii::app()->createUrl("/ClassBatch/batchDiscount"); ?>',
                    data: 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + '&batchId=' + batchId + '&seats=' + seats,
                    success: function(data) {
                        $(".totalBatchseatsPrice").html('');
                        $(".totalBatchseatsPrice").html(data);
                    }});
            }
            function onstartdateChange(val) {
                var venueId = $(".venueDetail").val();
                $("#selected_venue_date_error").text("");
                $.ajax({
                    type: 'POST',
                    url: '<?php echo Yii::app()->createUrl("/ClassBatch/selectBatchdate"); ?>',
                    dataType: 'JSON',
                    data: 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + '&batchid=' + val + '&venueId=' + venueId + '&classId=<?php echo $classid; ?>',
                    success: function(data) {
                        batchTimeinselectBox(data);

                    }
                });
            }

            //show all batch details
            function showBatchDeatils(venueId, classId) {
                jQuery.ajax({
                    'type': 'POST',
                    'url': "<?php echo Yii::app()->createAbsoluteUrl("/ClassVenue/getBatchDetails"); ?>",
                    'data': 'YII_CSRF_TOKEN=' + $('input[name="YII_CSRF_TOKEN"]').val() + "&venueId=" + venueId + '&classId=' + classId,
                    'beforeSend': function(jqXHR, settings) {
                        //imageloding();
                    },
                    'success': function(data) {
                        $("#venueBatchDetails").html('');
                        $("#venueBatchDetails").html(data);
                        //$(document).ajaxStop($.unblockUI);
                    },
                    'cache': false
                });
            }
            function BookNow(VenueId, BatchId) {
                venueBatchChange(VenueId);
                getVenueDetails(VenueId);
                $("#ShoppingCart_venue_id").val(VenueId);
                $("#batch_start_date").val(BatchId);
                onstartdateChange(BatchId)
            }
             $(document).bind('keypress', function(e){
  if(e.which === 13) { // return    
     return false;
  }
});

</script>